package com.tensquare.user.service;


import com.tensquare.user.dao.UserDao;
import com.tensquare.user.pojo.User;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private BCryptPasswordEncoder encoder;

    @Autowired
    private HttpServletRequest request;

    /**
     * 登录功能
     */
    public User login(String mobile,String password){
        User userLogin = userDao.findByMobile(mobile);
        if (null != userLogin && encoder.matches(password,userLogin.getPassword())){
            return userLogin;
        }
        return null;
    }


    /**
     *
     * 查询全部列表
     */

    public List<User> findAll(){
        return userDao.findAll();
    }

    /**
     *条件查询+分页
     *
     */


    /**
     *
     * 条件查询
     */


    /**
     *根据id查一条
     */

    public User findById(String id){
        return userDao.findById(id).get();
    }


    /**
     *注册（添加用户）
     */

    public void add(User user){
        user.setId(idWorker.nextId()+"");
        user.setFollowcount(0);
        user.setFanscount(0);
        user.setOnline(0L);
        user.setRegdate(new Date());
        user.setUpdatedate(new Date());
        user.setLastdate(new Date());
        user.setPassword(encoder.encode(user.getPassword()));
        userDao.save(user);
    }


    /**
     * 修改
     */

    public void update(User user){
        userDao.save(user);
    }

    /**
     * 删除
     */
    public void deleteById(String id){
        String token = (String) request.getAttribute("claims_admin");
        if (null == token || "".equals(token)){
            throw new RuntimeException("权限不足!");
        }
        userDao.deleteById(id);
    }

    //发送验证码
    public void sendSms(String mobile) {
        //生成六位随机数
        String checkcode = RandomStringUtils.randomNumeric(6);
        //想缓存放一份
        redisTemplate.opsForValue().set("checkcode_"+mobile,checkcode,5, TimeUnit.MINUTES);
        //给用户发一份
        Map<String,String> map = new HashMap<String, String>();
        map.put("mobile",mobile);
        map.put("checkcode",checkcode);
        rabbitTemplate.convertAndSend("sms",map);
        //在控制台显示一份[方便测试]
        //System.out.println("验证码为："+mobile+"---"+checkcode);
    }

    /**
     * 动态条件构建
     */

    //更改粉丝数和关注数
    @Transactional
    public void updateFansCountAndFollowCount(int x,String userid,String friendid){
        userDao.updateFansCount(x, friendid);
        userDao.updateFollowCount(x, userid);
    }

}
